package com.cn.goodsadmin.controller;


import com.cn.goodsadmin.po.Attachgoods;
import com.cn.goodsadmin.service.AttachGoodsService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class AttachGoodsController {

    @Autowired
    AttachGoodsService attachGoodsService;

    @LoginRequired
    @RequestMapping(value = "/addattchgoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addAttachGoodsInfo(@RequestBody Attachgoods attachgoods) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            attachGoodsService.save(attachgoods);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updateattchgoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateAttchgoods(@RequestBody Attachgoods attachgoods) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            attachGoodsService.update(attachgoods);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteattchgoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteAttchgoods(@RequestBody Attachgoods attachgoods) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            attachGoodsService.delete(attachgoods);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/allattchgoods", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap findAllAttachGoods(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
           List<Attachgoods> attachgoods= attachGoodsService.findAllAttachGoods(appid);
           if(attachgoods!=null&&attachgoods.size()>0){
                supplyMap.put("attachgoods",attachgoods);
           }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
